package com.gklyj.controller.zhaoping;

import com.gklyj.pojo.Position;
import com.gklyj.service.positionService;
import com.gklyj.service.userService;
import com.gklyj.service.user_zhanghaoService;
import com.gklyj.service.zhanghaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@Transactional
public class GangweiController {

    @Autowired
    positionService positionService;
    @Autowired
    private com.gklyj.service.userService userService;

    /*
    招聘单位登录点击岗位管理，跳转岗位管理页面
     */
    @GetMapping("/toZpGangweiIndex")
    public String toGangweiPage(){
        return "zhaoping/zp-gangwei";
    }

    /*
    列出当前登录的招聘单位所发布的岗位信息
     */
    @GetMapping("/ListThisZpGangwei")
    @ResponseBody
    public List<Position> ListThisAdGangweiData(HttpServletRequest request){
        //获取当前登录的招聘单位信息
        String username= (String) request.getSession().getAttribute("username");
        String department = userService.findDepartmentByUsername(username);
        return positionService.ListThisGangwei(department);
    }

    /*
    当前登录的招聘单位根据条件查询自身发布的岗位信息
     */
    @PostMapping("/thisZpGangweiSearch")
    @ResponseBody
    public List<Position> thisAdGangweiSearch(HttpServletRequest request, @RequestBody Position position){
        //获取当前登录的招聘单位信息
        String username= (String) request.getSession().getAttribute("username");
        String department = userService.findDepartmentByUsername(username);
        position.setDepartment(department);
        return positionService.ThisZpGangweiSearchs(position);
    }

    /*
    跳转编辑岗位信息页面
     */
    @GetMapping("/toZpGangweiEditPage")
    public String toZpGangweiEditPage(@RequestParam("id") String id, Model model){
        Position position = positionService.getById(Integer.valueOf(id));
        model.addAttribute("Position",position);
        return "zhaoping/zp-gangwei-edit";
    }

    /*
    跳转添加岗位信息页面
     */
    @GetMapping("/toZpGangweiAdd")
    public String toZpGangweiAddPage(){
        return "zhaoping/zp-gangwei-add";
    }

    /*
    招聘单位添加岗位信息
     */
    @PostMapping("/addZpGangwei")
    @ResponseBody
    public boolean AddZpGangwei(HttpServletRequest request,@RequestBody Position position) throws RuntimeException{
        //获取当前登录的招聘单位的 单位名称 与登陆人姓名
        String username= (String) request.getSession().getAttribute("username");
        String department = userService.findDepartmentByUsername(username);
        String name = userService.findNameByUsername(username);
        position.setDepartment(department);
        position.setName(name);
        //添加岗位创建时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//定义格式，不显示时分秒
        Timestamp now= new Timestamp(System.currentTimeMillis());//获取系统当前时间
        String str= df.format(now);
        position.setTime(str);
        position.setQuantity(0);
        return positionService.save(position);
    }
}
